package com.retoc.workflowable.controller;


import com.retoc.workflowable.common.R;
import com.retoc.workflowable.pojo.ClaimReq;
import com.retoc.workflowable.pojo.OtReq;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/claim")
public class ClaimController {
    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    HistoryService historyService;

    //创建请假申请
    @PostMapping("/claimRequest")
    public R claimRequest(@Valid @RequestBody ClaimReq req){
        Map<String,Object> map=new HashMap<>();
        map.put("url", req.getUrl());
        map.put("createrId", req.getCreaterId());
        map.put("managerId", req.getManagerId());
        map.put("gmId", req.getGmId());
        map.put("financeId", req.getFinanceId());
        map.put("needGm", req.getNeedGm());
        map.put("total", req.getTotal());
        map.put("types", req.getTypes());
        map.put("uuid", req.getUuid());

        ProcessInstance claimReq = runtimeService.startProcessInstanceByKey("claimReq", map);
//        leaveReq.getId()
        Task task = taskService.createTaskQuery()
                .processInstanceId(claimReq.getId())
                .taskAssignee(req.getCreaterId()+"")
                .singleResult();
        taskService.complete(task.getId());
        Map<String,Object> result=new HashMap<>();
        result.put("instanceId",claimReq.getId());
        result.put("types", req.getTypes());
        result.put("uuid", req.getUuid());
        return R.ok().put("result",result);
    }
}
